<template>
  <div class="app-container">
    <div class="zd3">
      <div class="zd3-title">
        <p>
          <span>角色描述</span>
          <input v-model="roleDescribe" type="text" placeholder="请输入角色描述" />
        </p>
        <h3 @click="getList">查询</h3>
        <h4 @click="resetList">重置</h4>
				<h3 @click="addPermission" style="background: #0093FF">新增</h3>
      </div>
      <div class="zd3-btm">
        <!-- <div class="zd3-btm-title">
          <p style="width: 80px;">序号</p>
          <p style="flex: 1;">权限名</p>
          <p style="flex: 1;">权限描述</p>
          <p style="width: 250px;">操作</p>
        </div>
        <div class="addDate" @click="addPeople">+ 新增成员</div>-->
        <el-table
          :data="tableData"
          style="width: 100%;border-radius: 8px;"
          :show-header="showHeader"
          v-loading="loading"
        >
          <el-table-column type="index" align="center" label="序号" width="80" />
          <el-table-column align="center" prop="roleName" label="角色名" />
          <el-table-column align="center" prop="roleDescribe" label="角色描述" />
          <el-table-column align="center" width="250" label="操作">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="changeRole(scope.row)">修改权限</el-button>
              <el-button type="text" size="small" @click="write(scope.row)">编辑</el-button>
              <el-popconfirm
                placement="top"
                hideIcon
                title="是否要删除此行？"
                @onConfirm="deleteRow(scope.row)"
              >
                <el-button type="text" size="small" slot="reference">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          hide-on-single-page
          layout="prev, pager, next"
          :total="total"
          @prev-click="goPrev"
          @next-click="goNext"
        />
      </div>
    </div>
    <el-dialog
      :visible.sync="dialogVisible"
      :modal-append-to-body="false"
			:close-on-click-modal="false"
      :modal="true"
      width="70%"
    >
      <div class="rolestip">
        <el-transfer
          filterable
          filter-placeholder="请输入权限"
          v-model="value"
          @change="changeUserRole"
          :props="{
						key: 'value',
						label: 'desc'
					}"
          :titles="['所有权限', '已有权限']"
          :button-texts="['删除', '新增']"
          :data="roledata"
        ></el-transfer>
      </div>
      <!-- <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save">保存</el-button>
      </div>-->
    </el-dialog>
    <el-dialog :visible.sync="jueseChange" :modal-append-to-body="false" :close-on-click-modal="false" :modal="true" width="70%">
      <div class="change">
        <p>
          <span>角色名</span>
          <el-select v-model="tempData.roleName" placeholder="请选择角色名" class="userselect">
            <el-option
              v-for="item in roleArr"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </p>
        <p>
          <span>角色说明</span>
          <input v-model="tempData.roleDescribe" type="text" placeholder="请输入角色说明" />
        </p>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="jueseChange = false">取消</el-button>
        <el-button type="primary" :disabled="isClick" @click="save">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getRole,
  updateRole,
  deleteRole,
  getRolePermission,
  deleteRolePermission,
  getUserPermission,
  jueseChaQuanxian,
  addRolePermissionCeshi
} from "@/api/people";
let cities = [],
  roleId = null,
  tempValue = null;
export default {
  data() {
    return {
      roleDescribe: "",
      total: 1,
      showHeader: true,
      loading: true,
      tableData: [],
      value: [],
      dialogVisible: false,
      jueseChange: false,
      tempData: "",
      tempIndex: -1,
      roledata: null,
			roleArr: [{
        label: '超级管理员',
        value: 'superAdmin'
      },{
        label: '管理员',
        value: 'admin'
      },{
        label: '普通用户',
        value: 'editor'
      },{
        label: '乳酸厂标签用户',
        value: 'rsEditor'
      },{
        label: '乳酸厂标签管理员',
        value: 'rsAdmin'
      },{
        label: '淀粉厂总监级',
        value: 'dfZongjian'
      },{
        label: '淀粉厂仓库管理员',
        value: 'dfAdmin'
      },{
        label: '淀粉厂车间级',
        value: 'dfChejian'
      },{
        label: '淀粉厂维修工',
        value: 'dfEditor'
      },{
        label: '内贸提成管理员',
        value: 'nmAdmin'
      },{
        label: '外贸提成管理员',
        value: 'wmAdmin'
      },{
        label: '投票管理员',
        value: 'tpAdmin'
      }],
			isClick: false
    };
  },
  mounted() {
    this.getList();
    this.getPermission();
  },
  methods: {
    // 获取总权限
    getPermission() {
      getUserPermission({}).then(res => {
        console.log("权限列表");
        console.log(res);
        cities = res.data;
        const data = [];
        res.data.forEach((role, index) => {
          data.push({
            desc: role.permissionName,
            value: role.permissionId
          });
        });
        this.roledata = data;
      });
    },
    // 获取当前用户权限
    getSelfPer(roleid) {
      jueseChaQuanxian({ roleId: roleid }).then(res => {
        console.log("现有权限");
        console.log(res);
        this.value = [];
        this.roleList = res.data;
        console.log(cities);
        let temparr = this.unique(res.data);
        console.log("temparr", temparr);
        // 对比所有权限,有权限放到右边
        for (let i = 0; i < cities.length; i++) {
          for (let j = 0; j < temparr.length; j++) {
            if (cities[i].permissionName == temparr[j].permissionName) {
              console.log(temparr[j].permissionName);
              this.value.push(temparr[j].permissionId);
            }
          }
        }
        console.log(this.value);
        tempValue = this.value;
      });
    },

    getList() {
      const param = {
        roleDescribe:this.roleDescribe ? this.roleDescribe : null
      };
      getRole(param).then(res => {
        console.log(res);
        this.tableData = res.data;
        this.loading = false;
      });
    },
    write(item) {
      console.log(item);
      this.jueseChange = true;
      this.tempData = { ...item };
      // this.value = []
      // this.$router.push({
      // 	name: 'AcademicWrite',
      // 	params: {
      // 		options: JSON.stringify(item)
      // 	}
      // })
    },
    // 开始修改权限 看后台是否能够数组去重
    changeUserRole(res, leftOrright) {
      console.log(leftOrright);
      this.value = res;
      console.log(res);
      console.log(this.value);
      if (leftOrright == "right") {
        console.log("新增");
        let newArr1 = tempValue.concat(res).filter(function(v, i, arr) {
          return arr.indexOf(v) === arr.lastIndexOf(v);
        });
        let permission = [];
        newArr1.map(item => {
          permission.push({
            permissionId: item
          });
        });
        addRolePermissionCeshi({
          roleId: roleId,
          userPermissions: permission
        }).then(res => {
          console.log(res);
          this.$message({
            showClose: true,
            message: res.message,
            type: "success"
          });
        });
      } else {
        let userRolePermission = [];
        console.log("tempValue:" + tempValue);
        console.log("res:" + res);
        // 对比两个数组取出不相同的部分
        let newArr = tempValue.concat(res).filter(function(v, i, arr) {
          return arr.indexOf(v) === arr.lastIndexOf(v);
        });
        console.log("newArr:" + newArr);
        newArr.map(item => {
          userRolePermission.push({
            permissionId: item,
            roleId: roleId
          });
        });
        deleteRolePermission(userRolePermission).then(res => {
          console.log(res);
          this.$message({
            showClose: true,
            message: res.message,
            type: "success"
          });
        });
      }
    },
    // 修改权限弹窗
    changeRole(item) {
      console.log("权限ID" + item.roleId);
      roleId = item.roleId;
      this.dialogVisible = true;
      console.log(this.value)
      this.getSelfPer(item.roleId);
    },
    save() {
			this.isClick = true
      updateRole(this.tempData).then(res => {
        console.log(res);
        this.$message({
          showClose: true,
          message: res.message,
          type: "success"
        });
        this.getList();
        this.jueseChange = false;
				this.isClick = false
      });
    },
    // 数组去重
    unique(arr) {
      var result = [];
      for (var i = 0; i < arr.length; i++) {
        var flag = true;
        for (var j = 0; j < result.length; j++) {
          if (arr[i].permissionName === result[j].permissionName) {
            flag = false;
          }
        }
        if (flag) {
          result.push(arr[i]);
        }
      }
      return result
    },
    //   save(item) {
    // 	console.log(this.value)
    // 	if(this.tempIndex == -1){
    // 		addRole(this.tempData).then(res => {
    // 			console.log(res)
    // 			this.$message({
    // 				showClose: true,
    // 				message: res.message,
    // 				type: 'success'
    // 			})
    // 			this.getList()
    // 		})
    // 	}else{
    // 		updateRole(this.tempData).then(res => {
    // 			console.log(res)
    // 			this.$message({
    // 				showClose: true,
    // 				message: res.message,
    // 				type: 'success'
    // 			})
    // 			this.getList()
    // 		})
    // 	}
    // 	this.dialogVisible = false
    // 	this.tempIndex = -1
    // },
    deleteRow(item) {
      console.log("删除");
      deleteRole(item.roleId).then(res => {
        console.log(res);
        this.$message({
          showClose: true,
          message: res.message,
          type: "success"
        });
        this.getList();
      });
    },
    goPrev(page) {
      console.log(page);
    },
    goNext(page) {
      console.log(page);
    },
    resetList() {
      this.roleDescribe = "";
    },
    addPermission(item) {
      this.$router.push({
        name: "AcademicWrite"
      });
    }
  }
};
</script>

<style lang="scss" scoped>
/deep/.el-button--primary {
  color: #fff;
  background-color: #18a05e !important;
  border-color: #18a05e !important;
}
/deep/.el-button--primary:hover,
.el-button--primary:focus {
  background: #18a05e !important;
  border-color: #18a05e !important;
  color: #fff !important;
}
/deep/.el-button:hover,
.el-button:focus {
  color: #18a05e;
  background-color: transparent;
}
/deep/.el-button--primary {
  color: #fff;
  background-color: #18a05e;
  border-color: #18a05e;
}
/deep/.el-button--text {
  color: #00c666 !important;
}
.app-container {
  padding: 10px;
}
.rolestip {
  width: 100%;
  display: flex;
  justify-content: center;
}
.inps {
  width: 100%;
  height: 32px;
  line-height: 32px;
  padding-left: 10px;
  background: none;
  border: none;
  color: #333;
}
.inps-active {
  width: 100%;
  height: 32px;
  line-height: 32px;
  padding-left: 10px;
  // background:#ccc;
  border: none;
  color: #333;
}
.zd3-title {
	height: 80px;
	display: flex;
	align-items: center;
	padding: 0 20px;
	position: relative;
	background: #ffffff;
	border-radius: 8px;
	border: 1px solid rgba(0, 0, 0, 0.1);

	> p {
		font-size: 14px;
		color: #333;
		margin-right: 30px;

		> span {
			padding-right: 20px;
		}

		> input {
			width: 200px;
			height: 32px;
			// background: #ccc;
			border-radius: 3px;
			border: 1px solid #ccc;
			padding-left: 10px;
			color: #333;
		}
	}

	> h3 {
		width: 64px;
		height: 32px;
		background: #18a05e;
		border-radius: 3px;
		margin: 0 30px 0 0;
		color: #fff;
		font-size: 14px;
		line-height: 32px;
		font-weight: 100;
		text-align: center;
		cursor: pointer;
	}

	> h4 {
		width: 64px;
		height: 32px;
		border-radius: 3px;
		border: 1px solid rgba(0, 0, 0, 0.1);
		color: #333;
		font-size: 14px;
		line-height: 32px;
		font-weight: 100;
		text-align: center;
		cursor: pointer;
		margin-right: 30px;
	}
}
.zd3-btm /deep/.btn-prev:after {
  content: "上一页";
}
.zd3-btm /deep/.btn-next:before {
  content: "下一页";
}
.zd3-btm /deep/.btn-prev,
.zd3-btm /deep/.btn-next {
  width: 65px;
}
.zd3-btm {
  background: rgba(255, 255, 255, 1);
  border-radius: 8px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  margin-top: 10px;
  overflow: hidden;
  &-title {
    display: flex;
    align-items: center;
    height: 70px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);
    background: #ffffff;
    > p {
      text-align: center;
      font-size: 16px;
      color: #333;
    }
  }
  .el-pagination {
		white-space: nowrap;
		padding: 15px 5px;
		color: #303133;
		font-weight: bold;
	}
  .addDate {
    margin: 20px 32px 10px 32px;
    height: 32px;
    border-radius: 3px;
    text-align: center;
    line-height: 32px;
    font-size: 14px;
    color: #333;
    cursor: pointer;
    border: 1px dashed rgba(0, 0, 0, 0.25);
  }
  .el-button--text {
    margin: 0 15px;
  }
}
.change {
  display: flex;
  flex-wrap: wrap;
  > p {
    width: 50%;
    font-size: 14px;
    display: flex;
    align-items: center;
    color: #333;
    /deep/.el-input__inner {
      width: inherit;
      background: none;
      height: 32px;
      // line-height: 32px;
      color: #333;
      border: 1px solid rgba(0, 0, 0, 0.1);
      padding: 0 25px;
    }
    /deep/.el-input__icon {
      line-height: 32px;
    }
    .userselect {
      // max-width: 250px;
      flex: 1;
    }

    > span {
      display: block;
      width: 120px;
      text-align: right;
      padding-right: 15px;
    }
    > input {
      width: inherit;
      flex: 1;
      height: 32px;
      border-radius: 3px;
      border: 1px solid rgba(0, 0, 0, 0.1);
      padding-left: 10px;
      color: #333;
    }
  }
}
</style>
